package com.codeczx.retrofitsource;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;


import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        testRetrofitCall();
        testRetrofitCallByRxJava();
    }

    private void testRetrofitCallByRxJava() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        GithubService githubService = retrofit.create(GithubService.class);
        githubService.getReposByRx("codeczx")
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(reposEntities -> {
                    Log.i(TAG, "testRetrofitCallByRxJava: "+reposEntities.size());
                });
    }

    private void testRetrofitCall() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.github.com/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                GithubService githubService = retrofit.create(GithubService.class);
                Call<List<ReposEntity>> call = githubService.getRepos("codeczx");
                String response = null;
                try {
                    response = call.execute().body().toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "testRetrofitCall: " + response);
            }
        }).start();
    }
}
